package com.wanmait.movies.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.movies.dao.DiscussDao;
import com.wanmait.movies.dao.MovieDao;
import com.wanmait.movies.vo.Movie;
import com.wanmait.movies.vo.Pager;
import com.wanmait.movies.vo.User;

/**
 * Servlet implementation class SearchServlet
 */
@WebServlet("/SearchServlet")
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String action = request.getParameter("action");
		if("doSearch".equals(action)) {
			doSearch( request, response);
		}
		if("doSearchMore".equals(action)) {
			doSearchMore( request, response);
		}
		if("doSearchByType".equals(action)) {
			doSearchByType( request, response);
		}
		 
		 
	}
	protected void doSearchByType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tag = request.getParameter("key");
		Pager pager = new Pager();
		pager.setCurrentPager(Integer.parseInt(request.getParameter("currentPager")));//当前要看第几页
		pager.setPagerNum(MovieDao.getMovieDao().pagemaxbytype(tag));//一共有多少个电影出现在页里
		pager.setCountinPager(pager.getPagerNum()/8+(pager.getPagerNum()%8==0?0:1));//每一页8部电影，一共有几页
		ArrayList<Movie> movies =  MovieDao.getMovieDao().getSearchMovieByType(tag, pager.getCurrentPager());
		request.setAttribute("search_text", movies.get(0).getMovieTypeId().getMovieType());
		request.setAttribute("movies", movies);
		request.setAttribute("searchpager", pager);
		request.getRequestDispatcher("/searchpage.jsp").forward(request, response);
		 //根据电影类型显示电影，一页8
	}
	protected void doSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tag = request.getParameter("search_text");
		ArrayList<Movie> movies =  MovieDao.getMovieDao().getSearchMovieList(tag);
		Pager pager = new Pager();
		pager.setCurrentPager(1);//当前要看第几页
		pager.setPagerNum(10);//一共有多少个电影出现在页里
		pager.setCountinPager(1);//每一页10部电影，一共有几页
		request.setAttribute("ifpageuse", false);
		request.setAttribute("movies", movies);
		request.setAttribute("search_text", tag);
		request.setAttribute("searchpager", pager);
		request.getRequestDispatcher("/searchpage.jsp").forward(request, response);
		//搜索框搜索，设置出现十条，就不分页了
		
	}
	protected void doSearchMore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 显示更多电影
		String tag = request.getParameter("key");
		String search = null;
		ArrayList<Movie> movies = null;
		Pager pager = new Pager();
		int currentPager = Integer.parseInt(request.getParameter("currentPager"));
		pager.setPagerNum(MovieDao.getMovieDao().pagemax());//一共有多少个电影出现在页里
		pager.setCurrentPager(currentPager);//当前要看第几页
		
		if ("rank".equals(tag)) {
			movies =  MovieDao.getMovieDao().getSearchGradesMovieList(currentPager);	
			search = "排行榜";
		}
		if ("grades".equals(tag)) {
			movies =  MovieDao.getMovieDao().getSearchGradesMovieList(currentPager);	
			search = "评分最高";
		}
		if ("click".equals(tag)) {
			movies =  MovieDao.getMovieDao().getSearchClickMovieList(currentPager);		
			search = "点赞最多";
		}
		if ("willshow".equals(tag)) {
			movies =  MovieDao.getMovieDao().getSearchWillShowMovieList(currentPager);
			pager.setPagerNum(24);
			search = "即将上映";
		}
		
		pager.setCountinPager(pager.getPagerNum()/8+(pager.getPagerNum()%8==0?0:1));//每一页8部电影，一共有几页
		request.setAttribute("searchpager", pager);
		request.setAttribute("search_text", search);  
		request.setAttribute("movies", movies);
		request.getRequestDispatcher("/searchpage.jsp").forward(request, response);
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
